package com.gly091020.client;

import com.gly091020.EnglishFight;
import com.gly091020.util.EnglishFightFun;
import com.gly091020.util.EnglishFightQuestion;
import com.gly091020.util.NetworkID;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.sound.PositionedSoundInstance;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.text.Text;
import net.minecraft.util.math.random.Random;

public class EnglishFightClient implements ClientModInitializer {
    public static final EnglishFightHud hud = new EnglishFightHud();
    @Override
    public void onInitializeClient() {
        HudRenderCallback.EVENT.register((context, delta) ->
        {
            if(MinecraftClient.getInstance().player == null){return;}
            if(EnchantmentHelper.getLevel(EnglishFight.ENGLISH_FIGHT, MinecraftClient.getInstance().player.getMainHandStack()) > 0){
                EnglishFightQuestion question = (EnglishFightQuestion)MinecraftClient.getInstance().player.getAttached(EnglishFight.ENGLISH_QUESTION);
                if (question == null){
                    var p = PacketByteBufs.create();
                    p.writeInt(-1);
                    ClientPlayNetworking.send(NetworkID.SEND_ANSWER, p);
                    return;
                }
                hud.setTitle(Text.of(question.getTitle()));
                hud.setOptions(question.getItems().toArray(Text[]::new));
                hud.setRightIndex(question.getRightIndex());
                hud.render(context, delta);
            }
        });
        ClientPlayNetworking.registerGlobalReceiver(NetworkID.UPDATE_QUESTION, (minecraftClient, clientPlayNetworkHandler, packetByteBuf, packetSender) -> {
            if(MinecraftClient.getInstance().player == null){return;}
            MinecraftClient.getInstance().player.setAttached(EnglishFight.ENGLISH_QUESTION, EnglishFightFun.questionFromBuffer(packetByteBuf));
        });
        ComboHudRenderer.register();
        ComboHudRenderer.setMaxCombo(30);
        ClientPlayNetworking.registerGlobalReceiver(NetworkID.UPDATE_COMBO, (minecraftClient, clientPlayNetworkHandler, packetByteBuf, packetSender) -> ComboHudRenderer.setCombo(packetByteBuf.readInt()));
    }

    public static void switchAnswer(double scrollAmount){
        int i = (int)Math.signum(scrollAmount / 2);
        if(hud.getHighlightedSection() + i < 0){
            hud.setHighlightedSection(hud.getSectionCount() - 1);
        }else {
            hud.setHighlightedSection((hud.getHighlightedSection() + i) % hud.getSectionCount());
        }
        var p = PacketByteBufs.create();
        p.writeInt(hud.getHighlightedSection());
        ClientPlayNetworking.send(NetworkID.SEND_ANSWER, p);
        if(MinecraftClient.getInstance().player != null){
            MinecraftClient.getInstance().player.playSound(SoundEvents.UI_BUTTON_CLICK.value(), SoundCategory.PLAYERS, 1, 1);
        }
    }
}
